/**************************************************************************************
*
*	Copyright (C) 2009 - 2012 Brent Andrew O. Chua - See LICENSE.txt for more info.
*		
*	Filename	-	FactoryComponent.cpp
*
*	Comments	-	See FactoryComponent.h
*
**************************************************************************************/
#include "../Include/FactoryComponent.h"

namespace Pulse
{
	FactoryComponent::FactoryComponent( void )
	{

	}

	FactoryComponent::~FactoryComponent( void )
	{
		ClearCreators();
	}

	BOOL FactoryComponent::RemoveCreator( EComponent::Type type )
	{
		auto findIter = m_creators.Find( type );

		if ( findIter != m_creators.IteratorEnd() )
		{
			m_creators.Remove( findIter );
			return TRUE;
		}

		return FALSE;
	}

	void FactoryComponent::ClearCreators( void )
	{
		m_creators.Clear();
	}

	IComponent * FactoryComponent::Create( EComponent::Type type, Actor *pOwner )
	{
		auto findIter = m_creators.Find( type );

		if ( findIter != m_creators.IteratorEnd() )
		{
			return findIter->second( pOwner );
		}

		PSX_PushWarning( "No component creator registered. Returning null." );

		return PSX_NULL;
	}

	template < typename NativeScriptClass >
	ComponentNativeScript * FactoryComponent::CreateNativeScript( Actor *pOwner )
	{
		ComponentNativeScript *pScript = (ComponentNativeScript*)Create( EComponent::NATIVE_SCRIPT, pOwner );

		return pScript;
	}

}